package cn.tedu.eguser.controller;

import cn.tedu.eguser.basic.response.JsonResult;
import cn.tedu.eguser.mapper.UserMapper;
import cn.tedu.eguser.pojo.dto.User;
import cn.tedu.eguser.pojo.dto.UserUpdate;
import cn.tedu.eguser.pojo.vo.UserListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
@RestController
@RequestMapping("/v1/user/")
@Validated
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("add")
    @Operation(summary = "添加用户功能")
    @ApiOperationSupport(order = 10)
    public JsonResult UserAddParam(@RequestBody @Validated User user){
        System.out.println("user:"+user);
        int i= userMapper.insertUser(user);
        System.out.println("受影响的行数："+i);
        return JsonResult.ok();
//        return "添加用户成功";
    }
    @GetMapping("list")
    public List<UserListVO> userListVO (String username){
        System.out.println("根据"+username+"进行查询");
        List<UserListVO> userlistVOS= userMapper.selectUser(username);
        for (UserListVO users: userlistVOS){
            System.out.println(users);
        }
        return userlistVOS;
    }
    @Operation(summary = "删除用户功能")
    @ApiOperationSupport(order = 20)
    @PostMapping("del/{id}")
    public JsonResult delUSer(@Range(min = 1L,max = 100L,message = "id 必须在1-100之间") @PathVariable Integer id ){
        userMapper.delUser(id);
        return JsonResult.ok("删除用户成功");
//        return "删除用户成功";
    }
    @PostMapping("update")
    public JsonResult UserUpdateParam(@RequestBody UserUpdate userUpdate){
        System.out.println("userUpdate:"+userUpdate);
        userUpdate.setCreateTime(new Date());
        int num= userMapper.updateUser(userUpdate);
        System.out.println("num:"+num);
        return JsonResult.ok("更新用户成功");
//        return "更新用户成功";
    }


}
